package com.ruoyi.core.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.core.domain.BatchClientUserSummary;
import com.ruoyi.core.domain.custombean.ClientUserBatchQuery;
import com.ruoyi.core.domain.custombean.ClientUserBatchQueryResult;
import com.ruoyi.core.service.BatchClientUserSummaryService;
import com.ruoyi.core.vo.BatchClientUserCreateOperationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Description:
 * @Company: 北京卡拉卡尔股份技术有限公司
 * @Author: Darren Ho
 * @Date: 10/16/2020 15:01
 */

@Api("客户端用户批量信息管理")
@RestController
@RequestMapping("/batch-client-user-summaries")
public class BatchClientUserController extends BaseController {

    @Autowired
    private BatchClientUserSummaryService batchClientUserSummaryService;

    /**
     * 新增用户
     */
    @ApiOperation("批量客户端用户创建")
    @PreAuthorize("@ss.hasRole('admin')")
    @PostMapping
    public AjaxResult create(@Validated @RequestBody BatchClientUserCreateOperationVO batchClientUserCreateOperationVO) {

        try {
            return AjaxResult.success(batchClientUserSummaryService.create(batchClientUserCreateOperationVO));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 查询batch_core列表
     */
    @ApiOperation("批量客户端用户创建记录查询")
    @PreAuthorize("@ss.hasRole('admin')")
    @GetMapping
    public TableDataInfo list(BatchClientUserSummary batchClientUserSummary) {
        startPage();
        List<BatchClientUserSummary> list = batchClientUserSummaryService.findBatchClientUserSummary(batchClientUserSummary);
        return getDataTable(list);
    }

    /**
     * 导出core_detail列表
     */
    @Log(title = "客户端用户批量管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasRole('admin')")
    @ApiOperation("批量客户端用户创建记录导出")
    @GetMapping("/export")
    public AjaxResult export(@RequestParam(name = "ids", required = false) Long[] ids) {
        List<BatchClientUserSummary> results;
        if (ids != null && ids.length != 0) {
            results = batchClientUserSummaryService.findByIdSet(ids);
        } else {
            results = batchClientUserSummaryService.findBatchClientUserSummary(new BatchClientUserSummary());
        }
        ExcelUtil<BatchClientUserSummary> util = new ExcelUtil<>(BatchClientUserSummary.class);
        return util.exportExcel(results, "批量客户端用户创建记录");
    }

    /**
     * 删除
     */
    @Log(title = "客户端用户批量管理", businessType = BusinessType.DELETE)
    @ApiOperation("批量客户端用户删除")
    @PreAuthorize("@ss.hasRole('admin')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return AjaxResult.success(batchClientUserSummaryService.deleteByIdSet(ids));
    }

    /**
     * 批次删除
     */
    @Log(title = "客户端用户批量管理", businessType = BusinessType.DELETE)
    @ApiOperation("批次用户删除")
    @PreAuthorize("@ss.hasRole('admin')")
    @GetMapping("/batchRemove")
    public AjaxResult batchRemove(@RequestParam(name = "ids", required = false) Long[] ids) {
        return AjaxResult.success(batchClientUserSummaryService.updateStatusToDelete(ids[0]));
    }

    /**
     * 查询core_detail列表
     */

    @ApiOperation("批量客户端用户明细列表")
    @PreAuthorize("@ss.hasRole('admin')")
    @GetMapping("/{batchId}/client-users")
    public TableDataInfo list(@PathVariable("batchId") Long batchId, ClientUserBatchQuery clientUserBatchQuery) {
        if (clientUserBatchQuery == null) {
            clientUserBatchQuery = new ClientUserBatchQuery();
        }
        clientUserBatchQuery.setBatchId(batchId);
        startPage();
        List<ClientUserBatchQueryResult> list = batchClientUserSummaryService.findBatchClientUser(clientUserBatchQuery);
        return getDataTable(list);
    }

    @Log(title = "客户端用户批量管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasRole('admin')")
    @ApiOperation("批量客户端用户信息导出")
    @GetMapping("/{batchId}/client-users/export")
    public AjaxResult clientUserBatchExport(@PathVariable("batchId") Long batchId, @RequestParam(name = "ids", required = false) Long[] ids) {
        List<ClientUserBatchQueryResult> results;
        if (ids != null && ids.length != 0) {
            results = batchClientUserSummaryService.findClientUserByUserIdSet(ids);
        } else {
            ClientUserBatchQuery clientUserBatchQuery = new ClientUserBatchQuery();
            clientUserBatchQuery.setBatchId(batchId);
            results = batchClientUserSummaryService.findBatchClientUser(clientUserBatchQuery);
        }
        ExcelUtil<ClientUserBatchQueryResult> util = new ExcelUtil<>(ClientUserBatchQueryResult.class);
        return util.exportExcel(results, "客户端用户简要信息");
    }

    @ApiOperation("获取批次权限列表")
    @PreAuthorize("@ss.hasRole('admin')")
    @GetMapping("/getPermissionList")
    public AjaxResult getPermissionList(@RequestParam(name = "id") @NotNull(message = "参数缺失") Long id) {
        return AjaxResult.success(batchClientUserSummaryService.getPermissionList(id));
    }
}
